﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AgentQltyHistroyList : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.none };
            }
        }

        protected string SerialNumber
        {
            get {
                return GetRequestValue<string>("s", "");
            }
        }
        protected string id
        {
            get
            {
                return GetRequestValue<string>("id", "");
            }
        }
        protected string oldClaimsNumber
        {
            get
            {
                return GetRequestValue<string>("oldClaimsNumber", "");
            }
        }
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
          
            // 每页记录数
            Grid1.PageSize = PageSize;
            ddlGridPageSize.SelectedValue = PageSize.ToString();
            BindGrid();
        }


        protected string GetSQLWhere()
        {
            string SeaVal = SerialNumber;

            string wheresql = "QltyId<>" + id;

            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
           


            return wheresql;
        }

        private void BindGrid()
        {
            
            Grid1.DataSource = AgentQltyController.GetPagination(GetSQLWhere(), Grid1.PageIndex + 1, Grid1.PageSize, Grid1.SortField + " " + Grid1.SortDirection, ref TotalCount)[0];
            Grid1.RecordCount = TotalCount;
            Grid1.DataBind();

        }

        #endregion

        #region Events        

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }



        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            DataRowView dr = e.DataItem as DataRowView;
            try
            {
                string FaultDescriptionVal = dr["FaultDescription"].ToString();
                System.Web.UI.WebControls.Label hlopenClaims = Grid1.Rows[e.RowIndex].FindControl("hlFaultDescription") as System.Web.UI.WebControls.Label;

                hlopenClaims.Text = StringHelper.StripTags(FaultDescriptionVal, false);
                //
                // CheckPowerFailWithLinkButtonField(Grid1, "btnView");

            }
            catch
            {

            }
        }

        protected void rblSearchSvrAct_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ChangeClaimsNumber")
            {
                if (string.IsNullOrEmpty(e.CommandArgument))
                {
                    return;//如果传入的参数为空,表示不能转成附属
                }
                string newClaimsNumber = AgentQltyController.GetNewClaimsNumber(e.CommandArgument);
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(newClaimsNumber)
                     + ActiveWindow.GetHideReference());
            }
        }

        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            DataRowView dr = e.DataItem as DataRowView;
            LinkButtonField ChangeClaimsNumberColumn = Grid1.FindColumn("ChangeClaimsNumberColumn") as LinkButtonField;

            //权限处理
            if (!HasPower("AgentQltySvrActList", new Permission[]{Permission.修改}))
                ChangeClaimsNumberColumn.Enabled = false;

            
            string ClaimsNumber = dr["ClaimsNumber"].ToString();
            if (ClaimsNumber.IndexOf("-") > 0)
            {                
                return;
            }
            ChangeClaimsNumberColumn.ToolTip = string.Format("可将品情{0}转换成此品情的附属品情", oldClaimsNumber);
            ChangeClaimsNumberColumn.ConfirmText = string.Format("你确定将品情{0}转换成品情{1}的附属品情吗？", oldClaimsNumber, ClaimsNumber);
            ChangeClaimsNumberColumn.CommandArgument=ClaimsNumber;//原品情号
        }

       
    }
}